﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using System.Diagnostics;

namespace LowLevelGraphics.ColorSpaces
{
    /// <summary>
    /// 
    /// </summary>
    [DebuggerDisplay("m: {m_fM}, s: {m_fS}")]
    public class L1Norme : AbstractColorSpace
    {
        protected float m_fS = 0.0f;
        protected float m_fM = 0.0f;

        /// <summary>
        /// 
        /// </summary>
        public L1Norme()
        {
        }

        /// <summary>
        /// Primary constructor
        /// </summary>
        public L1Norme(Color _color)
            : base(_color)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="sRGB"/> class.
        /// </summary>
        /// <param name="_fR">The _f R.</param>
        /// <param name="_fG">The _f G.</param>
        /// <param name="_fB">The _f B.</param>
        public L1Norme(float _fM, float _fS)
        {
            m_fM = _fM;
            m_fS = _fS;
        }

        /// <summary>
        /// this is used by clone to create a deep copy
        /// </summary>
        /// <param name="_sRGB"></param>
        internal L1Norme(L1Norme _sRGB)
        {
            this.m_fS = _sRGB.m_fS;
            this.m_fM = _sRGB.m_fM;
        }

        /// <summary>
        /// enables the caller to convert a color into sRGB
        /// </summary>
        /// <param name="_color"></param>
        /// <returns></returns>
        public static explicit operator L1Norme(Color _color)
        {
            return new L1Norme(_color);
        }

        /// <summary>
        /// 
        /// </summary>
        public float m
        {
            get { return m_fM; }
            set { m_fM = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public float s
        {
            get { return m_fS; }
            set { m_fS = value; }
        }

        /// <summary>
        /// This new chromatic representation has been very useful in brightness elimination
        /// of color images [12,13]. The intensity (achromatic signal) m and saturation signal s
        /// in the l1norme are calculated from r,g and b values of RGB, where the m Signal
        /// calculated is a normalization 0 m 255 of the achromatic axes of the RGB cube,
        /// and the s values are visual more agreeable with respect to the saturation of HLS
        /// or HSV spaces:
        /// </summary>
        protected override void CalcValues()
        {
            int r = m_Color.R;
            int g = m_Color.G;
            int b = m_Color.B;
            m_fM = 1 / 3 * (r + g + b);
            if ((b + r) >= 2 * g)
            {
                s = 3 / 2 * (r - m_fM);
            }
            if ((b + r) < 2 * g)
            {
                s = 3 / 2 * (m_fM - b);
            }
            m_fS = s;
        }

        /// <summary>
        /// converts back to RGB color
        /// </summary>
        /// <returns></returns>
        public override Color ToColor()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets values
        /// </summary>
        /// <returns></returns>
        public override object[] GetValues()
        {
            return new object[] { m_fM, m_fS };
        }

        /// <summary>
        /// Sets values
        /// </summary>
        /// <param name="_aObject"></param>
        public override void SetValues(object[] _aObject)
        {
            m_fM = (float)_aObject[0];
            m_fS = (float)_aObject[1];
        }
    }
}
